﻿function UpReturn(err,msg)
{
	var M=100;
	var Pic='T_PreviewPic';
	$("#T_PreviewPic").val(msg);
	if(err!=""){
		alert(err);
	}else if(msg!=''){
		$("#Preview").html('<div style="position:relative;float:left"><img src="'+webpath+'sys/images/icoClose.png" style="position:absolute;cursor:pointer;right:0;top:0" onclick="$(this).parent().remove();$(\'#'+Pic+'\').val(\'\');"/><img src="'+webconfig().upfolder+''+msg+'" style="max-height:'+M+'px; max-width:'+M+'px"/></div>');
	}
}
var cityxml = null;
function loadcity(f) {
    if (cityxml == null) {
        $.get(webpath + 'sys/xml/citylibrary.xml', function (xml) {
            cityxml = $(xml).find('row');
            cityxml.each(function () {
                $(this).attr({ t_name: $(this).find('t_name').text().toLowerCase(), t_english: $(this).find('t_english').text().toLowerCase(), t_para: $(this).find('t_para').text().toLowerCase() });
            });
            f();
        });
    } else { f(); };
}
$(document).ready(function () {
    $("#upload").attr("src", webpath + "Sys/iframeupload.aspx?r=" + $("#RanNum").val());
    UpReturn('', $('#T_PreviewPic').val());
    var y = '', i, x = new Date(Date.parse(webconfig().date.replace(/-/g, '/'))).getFullYear();
    for (x; x >= 1989; x--) {
        y += '<option value="' + x + '">' + x + '</option>';
    }
    $('#T_Year').html(y);
    y = $('#T_Year').attr('v');
    if (y != '') {
        $('#T_Year').val(y.substring(0, y.indexOf("-")));
        $('#T_Month').val(y.substring(y.indexOf("-") + 1));
    }
    AddEditor('T_Text', 'full', $('#RanNum').val());
    $("#TourExForm").validate({
        rules: {
            T_Title: { required: true },
            T_CityID: { required: true, min: 8 },
            T_SEOTitle: { required: function () { return $('#T_SEO').filter(':checked').length == 1 } },
            T_SEOKey: { required: function () { return $('#T_SEO').filter(':checked').length == 1 } },
            T_SEODesc: { required: function () { return $('#T_SEO').filter(':checked').length == 1 } },
            T_Text: { required: true },
            T_OrderID: { digits: true },
            T_DestinationID: { required: true, minlength: 2 }
        },
        messages: {
            T_Title: { required: '请填写标题' },
            T_CityID: { required: '请选择出发城市', min: '请选择出发城市' },
            T_SEOTitle: { required: '请填写SEO标题' },
            T_SEODesc: { required: '请填写SEO关键词' },
            T_SEODesc: { required: '请填写SEO描述' },
            T_Text: { required: '请填写内容' },
            T_OrderID: { digits: '必须是正整数!' },
            T_DestinationID: { required: '请填写目的地', minlength: '请填写目的地' }
        }
    });
    y = $('#T_CityID').val();
    CreateSelect(
                { Container: 'CityList', InputID: 'T_CityID', DefaultStr: '请选择', Type: 1, Path:webpath, Fu: function () {
                    $('#T_CityID').valid();
                }
                },
                { ID: (y == 0 ? 7 : y),dir:'', CategoryName: 'citylibrary', Start: 3, Stop: 4 }
            );
    if ($('#T_DestinationID').val() != '') {
        loadcity(function () {
            var x = $('#T_DestinationID').val().split(','), obj;
            for (i = 0; i < x.length - 1; i++) {
                if (x[i] != '') {
                    obj = cityxml.filter('[t_id="' + x[i] + '"]');
                    if (obj.length > 0) {
                        if (obj.attr('t_level') == 1) {
                            $('#dlist').append('<li v="' + x[i] + '">' + obj.find('t_name').text() + '(' + obj.find('t_name').text() + ')<b>×</b></li>')
                        } else {
                            $('#dlist').append('<li v="' + x[i] + '">' + obj.find('t_name').text() + '(' + cityxml.filter('[t_id="' + obj.attr('t_pid') + '"]').find('t_name').text() + ')<b>×</b></li>')
                        }
                    }
                }
            }
            $('#dlist>li>b').click(function () {
                var o = $(this).parent();
                $('#T_DestinationID').val($('#T_DestinationID').val().replace(',' + o.attr('v') + ',', ',')).valid();
                o.remove();
            });
        });
    }
    $('#positionlist').attr('v', '');
    $('#positionlist').append('<li id="no">没有找到相关目的地</li>');
    $('#T_Destination').focus(function () {
        $('#dmsg').hide();
        if ($(this).val() == '') {
            $('#positionlist').hide();
        } else {
            $('#positionlist').show();
        }
    }).blur(function () {
        setTimeout("$('#dmsg').hide();$('#positionlist').hide();", 500);
    }).keyup(function (e) {
        $('#dmsg').hide();
        var k = e.keyCode;
        if (k == 38 || k == 40) {
            return;
        } else if (k == 13) {
            $('#dadd').click(); return;
        }
        var v = $.trim($(this).val());
        $(this).val(v);
        if (v == '') { $('#positionlist').hide(); return; }
        var ov = $('#positionlist').attr('v');
        $('#positionlist').show();
        if (ov == '' || v.substring(0, ov.length) != ov) {
            $('#positionlist').html('<li class="load">数据加载中...</li>');
            loadcity(function () {
                var str = '', obj;
                if (v.match(/^([a-z]){1,4}$/)) {
                    var s = v.substring(0, 1), m = '[t_name^="' + s + '"]'; m1 = ',[t_english^="' + s + '"]';
                    for (var i = 1; i < v.length; i++) {
                        s = v.substring(i, i + 1);
                        m += '[t_name*="' + s + '"]'; m1 += '[t_english*="' + s + '"]';
                    }
                    obj = cityxml.filter(m + m1);
                } else {
                    obj = cityxml.filter('[t_name^="' + v + '"],[t_english^="' + v + '"]');
                }
                obj.each(function () {
                    str += '<li name="' + $(this).attr('t_name') + '" english="' + $(this).attr('t_english') + '" v="' + $(this).attr('t_id') + '"><em>';
                    if ($(this).attr('t_level') == 1) {
                        str += $(this).find('t_name').text();
                    } else {
                        str += cityxml.filter('[t_id="' + $(this).attr('t_pid') + '"]').find('t_name').text();
                    }
                    str += '</em><span>' + $(this).find('t_name').text() + '</span></li>';
                });
                $('#positionlist').html(str);
                $('#positionlist').attr('v', v);
                $('#positionlist li[v]').click(function () {
                    $(this).siblings().hide();
                    $(this).show();
                    $('#dadd').click();
                }).mouseover(function () {
                    $('#positionlist li[v]').removeClass('dselected');
                    $(this).addClass('dselected');
                });
            });
        } else {
            $('#positionlist li').hide();
            var obj = $('#positionlist li');
            if (v.match(/^([a-z]){1,4}$/)) {
                var s = v.substring(0, 1), m = '[name^="' + s + '"]'; m1 = ',[english^="' + s + '"]';
                for (var i = 1; i < v.length; i++) {
                    s = v.substring(i, i + 1);
                    m += '[name*="' + s + '"]'; m1 += '[english*="' + s + '"]';
                }
                obj = obj.filter(m + m1);
            } else {
                obj = obj.filter('[name^="' + v + '"],[english^="' + v + '"]');
            }
            if (obj.length == 0) {
                $('#no').show();
            } else {
                obj.show();
            }
        }
    }).keydown(function (e) {
        var k = e.keyCode;
        if (k == 38 || k == 40) {
            var o = $('#positionlist>li[v]:visible');
            if (o.length == 0) return;
            var obj;
            var s = o.filter('.dselected');
            if (s.length == 0) {
                obj = (k == 38 ? o.last() : o.first());
            } else {
                var j = o.index(s);
                if (k == 38) {
                    obj = (j == 0 ? o.last() : $(o).get(j - 1));
                } else {
                    obj = (j == o.length - 1 ? o.first() : $(o).get(j + 1));
                }
            }
            o.removeClass('dselected');
            $(obj).addClass('dselected');
            $('#positionlist').scrollTop($(obj).offset().top - $('#positionlist>li[v]:visible:first').offset().top);
        }
    });
    $('#dadd').click(function () {
        var o = $('#positionlist>li[v]:visible'), v, v1;
        if (o.length == 0) {
            $('#dmsg').html('没有相关目的地!').show(); return;
        }
        v = o.filter('.dselected');
        if (v.length == 0) v = o.first();
        o = v;
        v = $(v).attr('v');
        if ($('#dlist li[v="' + v + '"]').length == 1) {
            $('#dmsg').html('目的地重复!').show(); return;
        }
        v1 = $('#T_DestinationID').val();
        if (v1 == '') v1 = ',';
        $('#T_DestinationID').val(v1 + v + ',');
        $('#dlist').append('<li v=' + v + '>' + o.find('span').text() + '(' + o.find('em').text() + ')<b>×</b></li>').find('li[v="' + v + '"] b').click(function () {
            var o = $(this).parent();
            $('#T_DestinationID').val($('#T_DestinationID').val().replace(',' + o.attr('v') + ',', ',')).valid();
            o.remove();
        });
        $('#T_DestinationID').valid();
        $('#T_Destination').val('');
    });
});